package com.hyperzsoft.budget;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class DBProvider {
	OpenHelper DBHelper = null;
	
	private static final String DATABASE_NAME = "onabudget.db";
	private static final int DATABASE_VERSION = 3;
	private static final String TAG = "BudgetProvider";
	
	private static final String EXPENSE_TABLE_NAME = "Expense";
	private static final String EXPENSE_TYPE_TABLE_NAME = "ExpenseType";
	private static final String PERIOD_TABLE_NAME = "Period";
	private static final String PAYMENT_TABLE_NAME = "Expenses";
	
	public final int EXPENSE_TYPES = 0;
	public final int EXPENSE_GROUPS = 1;
	
	private static Context context;
	private static SQLiteStatement sqlStmt;
	private static SQLiteDatabase budgetDB;
	
	public enum BudgetTable {
		EXPENSE (EXPENSE_TABLE_NAME),
		EXPENSE_TYPE (EXPENSE_TYPE_TABLE_NAME),
		PERIOD (PERIOD_TABLE_NAME),
		PAYMENT (PAYMENT_TABLE_NAME);
		
		private BudgetTable(String aTable) {
			//can't call super ctor here
			fTable = aTable;
		}
		
		final String getTable() {
			return fTable;
		}

		private final String fTable;
	}
	
	public DBProvider(Context context) {
		try {
			DBProvider.context = context;
			DBHelper = new OpenHelper(DBProvider.context);
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
			Log.e(TAG, e.getStackTrace().toString());
		} 
	}
	
	//---opens the database---
    public DBProvider open() throws SQLException 
    {
    	budgetDB = DBHelper.getWritableDatabase();
        return this;
    }
    
    //---closes the database---    
    public void close() 
    {
    	DBHelper.close();
    }
	
	public void deleteAll(BudgetTable db_table) {
		budgetDB.delete(db_table.getTable(), null, null);
	}
	
	public ArrayList<Object> loadObjectList(BudgetTable db_table, Cursor cursor) {
		ArrayList<Object> list = new ArrayList<Object>();
		Expense expenseObj;
		ExpenseType expenseTypObj;
		Period periodObj;
		Payment paymentObj;
		
		try {
			if (cursor.moveToFirst()) {
				do {
					switch (db_table) {
						case EXPENSE:
							expenseObj = new Expense();
							expenseObj._ID = cursor.getLong(0);
							expenseObj.NAME = cursor.getString(1);
							expenseObj.TYPE_ID = cursor.getLong(2);
							expenseObj.GROUP_ID = cursor.getLong(3);
							expenseObj.AMOUNT = cursor.getDouble(4);
							expenseObj.EXPENSE_TYPE.selectSingleById(expenseObj.TYPE_ID, EXPENSE_TYPES);
							expenseObj.EXPENSE_GROUP.selectSingleById(expenseObj.GROUP_ID, EXPENSE_GROUPS);
							
							list.add(expenseObj);
							
							break;
						case EXPENSE_TYPE:
							expenseTypObj = new ExpenseType();
							expenseTypObj._ID = cursor.getLong(0);
							expenseTypObj.NAME = cursor.getString(1);
							expenseTypObj.COLOR = cursor.getInt(2);
							expenseTypObj.TYPE_OF = cursor.getInt(3);
							
							list.add(expenseTypObj);
							break;
						case PERIOD:
							periodObj = new Period();
							periodObj._ID = cursor.getLong(0);
							periodObj.START_DATE = cursor.getLong(1);
							periodObj.LABEL = cursor.getString(2);
							periodObj.EARNINGS = cursor.getDouble(3);
							periodObj.START_AMOUNT = cursor.getDouble(4);
							periodObj.END_AMOUNT = cursor.getDouble(5);
							periodObj.TOTAL_PAID = cursor.getDouble(6);
							periodObj.UPDATED_DATE = cursor.getLong(7);
							list.add(periodObj);
							break;
						case PAYMENT:
							paymentObj = new Payment();
							paymentObj._ID = (int) cursor.getLong(0);
							paymentObj.EXPENSE_ID = cursor.getLong(1);
							paymentObj.PERIOD_ID = cursor.getLong(2);
							paymentObj.AMOUNT_PAID = cursor.getDouble(3);
							
							list.add(paymentObj);
							break;
						default:
							throw new IllegalArgumentException("Invalid or NULL Database Table");
					}
				} while (cursor.moveToNext());
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
			Log.e(TAG, e.getStackTrace().toString());
		}
		
		return list;
	}
	
	public ArrayList<Object> selectAll(BudgetTable db_table, String sortBy, String selection, String[] selectionArgs) {
		Cursor cursor;
		String[] table_cols;
		ArrayList<Object> returnList = new ArrayList<Object>();
		
		try {
			if (sortBy == null) sortBy = "_id asc"; 
			
			switch (db_table) {
				case EXPENSE:
					table_cols = new String[] { "_id",
							context.getString(R.string.expense_name),
							context.getString(R.string.expense_type_id),
							context.getString(R.string.expense_group_id),
							context.getString(R.string.expense_amount) 
					};
					break;
				case EXPENSE_TYPE:
					table_cols = new String[] { "_id",
							context.getString(R.string.expense_type_name),
							context.getString(R.string.expense_type_color),
							context.getString(R.string.expense_type_typeof)
					};
					break;
				case PERIOD:
					table_cols = new String[] { "_id",
							context.getString(R.string.period_start_date),
							context.getString(R.string.period_label),
							context.getString(R.string.period_earnings),
							context.getString(R.string.period_start_amount),
							context.getString(R.string.period_end_amount),
							context.getString(R.string.period_total_paid),
							context.getString(R.string.period_updated_date)
					};
					break;
				case PAYMENT:
					table_cols = new String[] { "_id",
						context.getString(R.string.payment_expense_id),
						context.getString(R.string.payment_period_id),
						context.getString(R.string.payment_amount_paid)
					};
					break;
				default:
					throw new IllegalArgumentException("Invalid or NULL Database Table");
			}
			
			cursor = budgetDB.query(db_table.getTable(), table_cols, 
					selection, selectionArgs, null, null, sortBy);
			
			ArrayList<Object> list = loadObjectList(db_table, cursor);
			cursor.close();
			
			returnList = list;
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
			Log.e(TAG, e.getStackTrace().toString());
		}
		
		return returnList;
	}
	
	/**
	 * Methods calling this must use startManagingCursor
	 * @return Cursor of Types
	 */
	public Cursor getSpinnerCursorTypes() {
		Cursor cursor = null;
		
		try {
			cursor = budgetDB.rawQuery("SELECT _id, " 
					+ context.getString(R.string.expense_type_name) 
					+ " FROM " + EXPENSE_TYPE_TABLE_NAME 
					+ " WHERE type_of = ?", new String[] {Long.toString(EXPENSE_TYPES)});
		}
		catch(SQLException e) {
			Log.e(TAG, e.getMessage());
			Log.e(TAG, e.getStackTrace().toString());
		}
		
		return cursor;
	}
	
	/**
	 * Methods calling this must use startManagingCursor
	 * @return Cursor of Groups
	 */
	public Cursor getSpinnerCursorGroups() {
		Cursor cursor = null;
		
		try {
			cursor = budgetDB.rawQuery("SELECT _id, " 
					+ context.getString(R.string.expense_type_name) 
					+ " FROM " + EXPENSE_TYPE_TABLE_NAME 
					+ " WHERE type_of = ?", new String[] {Long.toString(EXPENSE_GROUPS)});
			
		}
		catch(SQLException e) {
			Log.e(TAG, e.getMessage());
			Log.e(TAG, e.getStackTrace().toString());
		}
		
		return cursor;
	}
	
	@SuppressWarnings("unused")
	public class OpenHelper extends SQLiteOpenHelper {
		private static final String TAG = "OpenHelper";
		private final Context context;
		
		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			this.context = context;
			
		}
		
		@Override
		public void onCreate(SQLiteDatabase budgetDB) {
			//Create all the tables
			budgetDB.execSQL("CREATE TABLE " + EXPENSE_TABLE_NAME + " ("
				+ "_id INTEGER PRIMARY KEY,"
				+ this.context.getString(R.string.expense_name) + " TEXT,"
				+ this.context.getString(R.string.expense_type_id) + " REAL,"
				+ this.context.getString(R.string.expense_group_id) + " REAL,"
				+ this.context.getString(R.string.expense_amount) + " REAL"
				+ ");"
			);
			
			budgetDB.execSQL("CREATE TABLE " + EXPENSE_TYPE_TABLE_NAME + " ("
				+ "_id INTEGER PRIMARY KEY,"
				+ this.context.getString(R.string.expense_type_name) + " TEXT,"
				+ this.context.getString(R.string.expense_type_color) + " INTEGER,"
				+ this.context.getString(R.string.expense_type_typeof) + " INTEGER"
				+ ");"
			);
			
			budgetDB.execSQL("CREATE TABLE " + PERIOD_TABLE_NAME + " ("
				+ "_id INTEGER PRIMARY KEY,"
				+ this.context.getString(R.string.period_start_date) + " INTEGER,"
				+ this.context.getString(R.string.period_label) + " TEXT,"
				+ this.context.getString(R.string.period_earnings) + " REAL,"
				+ this.context.getString(R.string.period_start_amount) + " REAL,"
				+ this.context.getString(R.string.period_end_amount) + " REAL,"
				+ this.context.getString(R.string.period_total_paid) + " REAL,"
				+ this.context.getString(R.string.period_updated_date) + " INTEGER"
				+ ");"
			);
			
			budgetDB.execSQL("CREATE TABLE " + PAYMENT_TABLE_NAME + " ("
				+ "_id INTEGER PRIMARY KEY,"
				+ this.context.getString(R.string.payment_expense_id) + " REAL,"
				+ this.context.getString(R.string.payment_period_id) + " REAL,"
				+ this.context.getString(R.string.payment_amount_paid) + " REAL"
				+ ");"
			);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase budgetDB, int oldVersion, int newVersion) {
			//for now just drop and recreate!
			//TODO: A proper DB upgrade
			budgetDB.execSQL("DROP TABLE IF EXISTS " + EXPENSE_TABLE_NAME);
			budgetDB.execSQL("DROP TABLE IF EXISTS " + EXPENSE_TYPE_TABLE_NAME);
			budgetDB.execSQL("DROP TABLE IF EXISTS " + PERIOD_TABLE_NAME);
			budgetDB.execSQL("DROP TABLE IF EXISTS " + PAYMENT_TABLE_NAME);
			
			onCreate(budgetDB);
		}
	}
	
	//TODO: implement Parcelable
	public class Expense { // implements Parcelable {
		public long _ID;
		public String NAME;
		public long TYPE_ID;
		public long GROUP_ID;
		public double AMOUNT;
		public ExpenseType EXPENSE_TYPE;
		public ExpenseType EXPENSE_GROUP;
//		
//		//parcelable methods
//		@Override
//		public int describeContents() {
//			return hashCode();
//		}
//		
//		@Override
//		public void writeToParcel(Parcel dest, int flags) {
//			
//		}
//		
//		public Expense(Parcel source) {
//			
//		}
//		
		public Expense() {
			this._ID = -1;		//init
			this.EXPENSE_TYPE = new ExpenseType();
			this.EXPENSE_GROUP = new ExpenseType();
		}
		
		public void selectSingleById(long idIndex, boolean shouldLoadSubObjects) {
			Cursor cursor = budgetDB.rawQuery("SELECT * FROM " 
					+ EXPENSE_TABLE_NAME + " WHERE _id = ?", 
					new String[] {Long.toString(idIndex)});
			
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					this._ID = (int) cursor.getLong(0);
					this.NAME = cursor.getString(1);
					this.TYPE_ID = cursor.getLong(2);
					this.GROUP_ID = cursor.getLong(3);
					this.AMOUNT = cursor.getDouble(4);
				}
			}
			
			cursor.close();
			
			if (shouldLoadSubObjects) {
				this.EXPENSE_TYPE.selectSingleById(TYPE_ID, EXPENSE_TYPES);
				this.EXPENSE_GROUP.selectSingleById(GROUP_ID, EXPENSE_GROUPS);
			} else {
				this.EXPENSE_TYPE = null;
				this.EXPENSE_GROUP = null;
			}
		}
		
		public void insert() {
			try {
				sqlStmt = budgetDB.compileStatement("INSERT INTO " + EXPENSE_TABLE_NAME + " ("
					+ context.getString(R.string.expense_name) + ","
					+ context.getString(R.string.expense_type_id) + ","
					+ context.getString(R.string.expense_group_id)  + ","
					+ context.getString(R.string.expense_amount) + ")"
					+ " VALUES (?,?,?,?)");
				
				sqlStmt.bindString(1, this.NAME);
				sqlStmt.bindLong(2, this.TYPE_ID);
				sqlStmt.bindLong(3, this.GROUP_ID);
				sqlStmt.bindDouble(4, this.AMOUNT);
				
				this._ID = sqlStmt.executeInsert();
				
			} catch (SQLException e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
			finally {
				this._ID = -1;
			}
		}
		
		public void update() {
			try {
				if (this._ID != -1) {
					try {
						ContentValues updateExpense = new ContentValues();
						updateExpense.put(context.getString(R.string.expense_name), this.NAME);
						updateExpense.put(context.getString(R.string.expense_type_id), Long.toString(this.TYPE_ID));
						updateExpense.put(context.getString(R.string.expense_group_id), Long.toString(this.GROUP_ID));
						updateExpense.put(context.getString(R.string.expense_amount), Double.toString(this.AMOUNT));
						budgetDB.update(EXPENSE_TABLE_NAME, updateExpense, "_id=?", new String[] {Long.toString(this._ID)});
						
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
		
		public void delete() {
			try {
				if (this._ID != -1) {
					try {
						budgetDB.delete(EXPENSE_TABLE_NAME, "_id=" + this._ID, null);
						
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
	}
	
	public class ExpenseType implements Parcelable {
		public long _ID;
		public String NAME;
		public int COLOR;
		public int TYPE_OF;
		
		//parcelable methods
		@Override
		public int describeContents() {
			return hashCode();
		}
		
		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeLong(this._ID);
			dest.writeString(this.NAME);
			dest.writeInt(this.COLOR);
			dest.writeInt(this.TYPE_OF);
		}
		
		public ExpenseType(Parcel source) {
			this._ID = source.readLong();
			this.NAME = source.readString();
			this.COLOR = source.readInt();
			this.TYPE_OF = source.readInt();
		}
		
		public final Parcelable.Creator<ExpenseType> CREATOR = new Parcelable.Creator<ExpenseType>() {
			public ExpenseType createFromParcel(Parcel in) {
				return new ExpenseType(in);
			}
			public ExpenseType[] newArray(int size) {
				return new ExpenseType[size];
			}
		};
		 
		public ExpenseType() {
			this._ID = -1;
		}
		
		public void selectSingleById(long idIndex, int typeOf) {
			Cursor cursor = budgetDB.rawQuery("SELECT * FROM " 
					+ EXPENSE_TYPE_TABLE_NAME + " WHERE _id = ? AND " 
					+ context.getString(R.string.expense_type_typeof) + " = ?", 
					new String[] {Long.toString(idIndex), Integer.toString(typeOf)});
			
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					this._ID = (int) cursor.getLong(0);
					this.NAME = cursor.getString(1);
					this.COLOR= (int) cursor.getLong(2);
					this.TYPE_OF = (int) cursor.getLong(3);
				}
			}
			
			cursor.close();
		}
		
		public void insert() {
			try {
				sqlStmt = budgetDB.compileStatement("INSERT INTO " + EXPENSE_TYPE_TABLE_NAME + " ("
						+ context.getString(R.string.expense_type_name) + ","
						+ context.getString(R.string.expense_type_color) + ","
						+ context.getString(R.string.expense_type_typeof) + ")" 
						+ " VALUES (?,?,?)");
				
				sqlStmt.bindString(1, this.NAME);
				sqlStmt.bindLong(2, this.COLOR);
				sqlStmt.bindLong(3, this.TYPE_OF);
				
				this._ID = sqlStmt.executeInsert();
				
			} catch (SQLException e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
				this._ID = -1;
			}
		}
		
		public void update() {
			try {
				if (this._ID != -1) {
					try {
						ContentValues updateExpenseType = new ContentValues();
						updateExpenseType.put(context.getString(R.string.expense_type_name), this.NAME);
						updateExpenseType.put(context.getString(R.string.expense_type_color), this.COLOR);
						updateExpenseType.put(context.getString(R.string.expense_type_typeof), this.TYPE_OF);
						budgetDB.update(EXPENSE_TYPE_TABLE_NAME, updateExpenseType, "_id=?", new String[] {Long.toString(this._ID)});
					    
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
		
		public void delete() {
			try {
				if (this._ID != -1) {
					try {
						if (!budgetDB.isOpen()) { open(); } 
						budgetDB.delete(EXPENSE_TYPE_TABLE_NAME, "_id=" + this._ID, null);
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
	}
	
	//TODO: implement Parcelable
	public class Period {
		public long _ID;
		public long START_DATE;
		public String LABEL;
		public double EARNINGS;
		public double START_AMOUNT;
		public double END_AMOUNT;
		public double TOTAL_PAID;
		public long UPDATED_DATE;

		public Period() {
			this._ID = -1;
		}
		
		public void selectSingleById(long idIndex) {
			Cursor cursor = budgetDB.rawQuery("SELECT * FROM " + PERIOD_TABLE_NAME + " WHERE _id = ?", new String[] {Long.toString(idIndex)});
			
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					this._ID = (int) cursor.getLong(0);
					this.START_DATE = cursor.getLong(1);
					this.LABEL= cursor.getString(2);
					this.EARNINGS = cursor.getDouble(3);
					this.START_AMOUNT = cursor.getDouble(4);
					this.END_AMOUNT = cursor.getDouble(5);
					this.TOTAL_PAID = cursor.getDouble(6);
					this.UPDATED_DATE = cursor.getLong(7);
				}
			}

			cursor.close();
		}
		
		public void insert() {
			try {
				sqlStmt = budgetDB.compileStatement("INSERT INTO " + PERIOD_TABLE_NAME + " ("
					+ context.getString(R.string.period_start_date) + ","
					+ context.getString(R.string.period_label) + ","
					+ context.getString(R.string.period_earnings) + ","
					+ context.getString(R.string.period_start_amount) + ","
					+ context.getString(R.string.period_end_amount) + ","
					+ context.getString(R.string.period_total_paid) + ","
					+ context.getString(R.string.period_updated_date) + ")"
					+ " VALUES (?,?,?,?,?,?,?)");
				
				sqlStmt.bindLong(1, this.START_DATE);
				sqlStmt.bindString(2, this.LABEL);
				sqlStmt.bindDouble(3, this.EARNINGS);
				sqlStmt.bindDouble(4, this.START_AMOUNT);
				sqlStmt.bindDouble(5, this.END_AMOUNT);
				sqlStmt.bindDouble(6, this.TOTAL_PAID);
				sqlStmt.bindLong(7, this.UPDATED_DATE);
				
				this._ID = sqlStmt.executeInsert();
				
			} catch (SQLException e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
				this._ID = -1;
			}
		}
		
		public void update() {
			try {
				if (this._ID != -1) {
					try {
						ContentValues updatePeriod = new ContentValues();
						updatePeriod.put(context.getString(R.string.period_start_date), this.START_DATE);
						updatePeriod.put(context.getString(R.string.period_label), this.LABEL);
						updatePeriod.put(context.getString(R.string.period_earnings), Double.toString(this.EARNINGS));
						updatePeriod.put(context.getString(R.string.period_start_amount), Double.toString(this.START_AMOUNT));
						updatePeriod.put(context.getString(R.string.period_end_amount), Double.toString(this.END_AMOUNT));
						updatePeriod.put(context.getString(R.string.period_total_paid), Double.toString(this.TOTAL_PAID));
						updatePeriod.put(context.getString(R.string.period_updated_date), Long.toString(this.UPDATED_DATE));
						budgetDB.update(PERIOD_TABLE_NAME, updatePeriod, "_id=?", new String[] {Long.toString(this._ID)});
					    
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
		
		public void delete() {
			try {
				if (this._ID != -1) {
					try {
						
						budgetDB.delete(PERIOD_TABLE_NAME, "_id=" + this._ID, null);
						
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
	}
	
	//TODO: implement Parcelable
	public class Payment {
		public long _ID;
		public long EXPENSE_ID;
		public long PERIOD_ID;
		public double AMOUNT_PAID;
		
		public Payment() {
			this._ID = -1;
		}
		
		public void insert() {
			try {
				sqlStmt = budgetDB.compileStatement("INSERT INTO " + PAYMENT_TABLE_NAME + " ("
						+  context.getString(R.string.payment_expense_id) + ","
						+  context.getString(R.string.payment_period_id) + ","
						+  context.getString(R.string.payment_amount_paid) + ")"
						+ " VALUES (?,?,?)");
					 
				 sqlStmt.bindLong(1, this.EXPENSE_ID);
				 sqlStmt.bindLong(2, this.PERIOD_ID);
				 sqlStmt.bindDouble(3, this.AMOUNT_PAID);
				 
				this._ID = sqlStmt.executeInsert();
				
			} catch (SQLException e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
				this._ID = -1;
			}
		}
		
		public void update() {
			try {
				if (this._ID != -1) {
					try {
						ContentValues updatePeriod = new ContentValues();
						updatePeriod.put(context.getString(R.string.payment_expense_id), Long.toString(this.EXPENSE_ID));
						updatePeriod.put(context.getString(R.string.payment_period_id), Long.toString(this.PERIOD_ID));
						updatePeriod.put(context.getString(R.string.payment_amount_paid), Double.toString(this.AMOUNT_PAID));
						
						budgetDB.update(PAYMENT_TABLE_NAME, updatePeriod, "_id=?", new String[] {Long.toString(this._ID)});
						
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
		
		public void delete() {
			try {
				if (this._ID != -1) {
					try {
						budgetDB.delete(PAYMENT_TABLE_NAME, "_id=" + this._ID, null);
						
					} catch (SQLException e) {
						Log.e(TAG, e.getLocalizedMessage());
						Log.e(TAG, e.getStackTrace().toString());
					}
				} else {
					throw new IllegalArgumentException("Invalid Object ID");
				}
			} catch (Exception e) {
				Log.e(TAG, e.getLocalizedMessage());
				Log.e(TAG, e.getStackTrace().toString());
			}
		}
	}
}
